<?php
/**
 * 业务员评价系统
 */
class MarketService
{
    public function add ($params, $uid)
    {
     
        $params['add_time'] = time();
        $params['uid'] = $uid;
        $params['month'] = date("Ym", time());
        $rs = MarketModel::instance()->addMarket($params);
        if ($rs > 0) {
            return array('status' => true, 'message' => "提交成功,感谢你的评价");
        } else {
            return array('status' => false, 'message' => "提交失败");
        }
    }
    //获取自己的区域经理
    public function getServerMan ($uid)
    {
        $member = new MemberService();
        $user = $member->getMemberByUid($uid);
        $erp = new ERPService();
        $erpUser = $erp->getUserByRealName($user['real_name']);
        if (empty($erpUser)) {
            return array('status' => false, 'message' => "你尚未在公司登记");
        }
        //获取区域
        $yw = $erp->getSalesmanByKHBH($erpUser['khbh']);
        if (empty($yw['ywym'])) {
            return array('status' => false, 'message' => "没有找到你的业务员");
        }
        $salesman = $member->getMemberByRealname($yw['ywym']);
        return array('status' => true, 'data' => $salesman);
    }
    /**
     * 
     * 检查是否已经评价过了
     * @param int $uid
     * @return boolean true 没有评论过 false 评价过了
     */
    public function isRating ($uid)
    {
        $month = date("Ym", time());
        $rs = $this->getMarketByUid($uid, $month);
        return empty($rs);
    }
    public function getMarketByUid ($uid, $month)
    {
        return MarketModel::instance()->getMarketByUid($uid, $month);
    }
    public function getMarket ($limit)
    {
        $rs = MarketModel::instance()->getMarket($limit);
        foreach ($rs as $k => $v) {
        
        }
    }
}
class MarketModel extends Db
{
    private $_table = 'd_market';
    public function addMarket ($params)
    {
        return $this->add($this->_table, $params);
    }
    public function getMarketByUid ($uid, $month)
    {
        return $this->getOne($this->_table, 
        array('uid' => $uid, 'month' => $month));
    }
    public function getMarket ($limit)
    {
        return $this->getAll($this->_table, null, null, 'id DESC', $limit);
    }
    /**
     * 
     * @return MarketModel
     */
    public static function instance ()
    {
        return parent::_instance(__CLASS__);
    }
}